﻿Imports Microsoft.VisualBasic
Imports System
Imports System.Data
Imports System.Configuration
Imports System.Web
Imports System.Web.Security
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports System.Collections.Generic
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports System.Data.Common

Public Class errorLog
#Region "Private"
    Private Shared _intErrorLog_Id As Integer = 0
    Private Shared _strMessage As String = String.Empty
    Private Shared _strSource As String = String.Empty
    Private Shared _strForm As String = String.Empty
    Private Shared _strQueryString As String = String.Empty
    Private Shared _strTargetSite As String = String.Empty
    Private Shared _strStackTrace As String = String.Empty
    Private Shared _strSessionKeys As String = String.Empty
#End Region
#Region "Public"
    Public Property ErrorId() As Integer
        Get
            Return _intErrorLog_Id
        End Get
        Set(ByVal value As Integer)
            _intErrorLog_Id = value
        End Set
    End Property
    Public Property Message() As String
        Get
            Return _strMessage
        End Get
        Set(ByVal value As String)
            _strMessage = value
        End Set
    End Property
    Public Property Source() As String
        Get
            Return _strSource
        End Get
        Set(ByVal value As String)
            _strSource = value
        End Set
    End Property
    Public Property Form() As String
        Get
            Return _strForm
        End Get
        Set(ByVal value As String)
            _strForm = value
        End Set
    End Property
    Public Property QueryString() As String
        Get
            Return _strQueryString
        End Get
        Set(ByVal value As String)
            _strQueryString = value
        End Set
    End Property
    Public Property TargetSite() As String
        Get
            Return _strTargetSite
        End Get
        Set(ByVal value As String)
            _strTargetSite = value
        End Set
    End Property
    Public Property StackTrace() As String
        Get
            Return _strStackTrace
        End Get
        Set(ByVal value As String)
            _strStackTrace = value
        End Set
    End Property
    Public Property SessionKeys() As String
        Get
            Return _strSessionKeys
        End Get
        Set(ByVal value As String)
            _strSessionKeys = value
        End Set
    End Property
#End Region
    Public Function CatchError(ByVal ex As Exception) As String
        _strMessage = CatchError(ex, False)
        Return _strMessage
    End Function
    Public Function CatchError(ByVal ex As Exception, ByVal _serverTransfer As Boolean) As String
        Dim db As Database = DbStoreFactory.CreateDatabase
        Dim cmd As DbCommand = db.GetStoredProcCommand("InsertWebError")
        Dim strErrorMessage As String = String.Empty

        _strMessage = ex.Message
        _strSource = ex.Source
        _strForm = HttpContext.Current.Request.Form.ToString
        _strQueryString = HttpContext.Current.Request.QueryString.ToString
        _strTargetSite = ex.TargetSite.ToString
        _strStackTrace = ex.StackTrace.ToString
        _strSessionKeys = GetSessionKeys()

        db.DiscoverParameters(cmd)
        cmd.Parameters("@Message").Value = _strMessage
        cmd.Parameters("@Source").Value = _strSource
        cmd.Parameters("@Form").Value = _strForm
        cmd.Parameters("@QueryString").Value = _strQueryString
        cmd.Parameters("@TargetSite").Value = _strTargetSite
        cmd.Parameters("@StackTrace").Value = _strStackTrace
        cmd.Parameters("@SessionKeys").Value = _strSessionKeys

        If Not (HttpContext.Current.Request.QueryString("error") Is Nothing) Then
            strErrorMessage = String.Empty
            Return strErrorMessage
        Else
            strErrorMessage = db.ExecuteScalar(cmd).ToString
            If Not (_serverTransfer) Then
                If Not (HttpContext.Current.Request.QueryString.ToString = String.Empty) Then
                    HttpContext.Current.Response.Redirect(System.Web.HttpContext.Current.Request.Url.PathAndQuery.ToString & "&error=" & strErrorMessage)
                Else
                    HttpContext.Current.Response.Redirect(System.Web.HttpContext.Current.Request.Url.PathAndQuery.ToString() & "?error=" & strErrorMessage)
                End If
            End If
        End If

        If Not (cmd Is Nothing) Then
            cmd.Dispose()
        End If

        Return strErrorMessage
    End Function
    Public Sub CheckError(ByVal ex As Exception)
        Dim el As New errorLog
        el.CatchError(ex)
    End Sub
    Public Function GetSessionKeys() As String
        Dim _strSession As String = String.Empty
        Dim _strKey As String

        For Each _strKey In HttpContext.Current.Session.Keys
            _strSession = _strSession & "Key(" & _strKey
            _strSession = _strSession & ") Value(" & HttpContext.Current.Session(_strKey).ToString & ") | "
        Next

        Return _strSession
    End Function
End Class
